from frappe.model.document import Document
from erpnext.selling.doctype.sales_order.sales_order import SalesOrder
from teo_custom.teo_naming import get_custom_series
from frappe.utils import getdate

class CustomSalesOrder(SalesOrder):
    def autoname(self):
        print("CustomSalesOrder autoname called")        
        date_obj = getdate(self.transaction_date)
        year_month = date_obj.strftime("%y%m")
        name_class = date_obj.strftime("%Y")
        naming_series = self.naming_series.replace(".YYYY.", year_month)
        series_key = get_custom_series(self.doctype,name_class,6)
        self.name = f"{naming_series}{series_key}"
